package com.example.Servlet;

import com.example.Pojo.Member;
import com.example.Pojo.TimesGet;
import com.example.dao.impl.UserDaoImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

public class InvestMoney extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*  已经配置了解决乱码问题的过滤器
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        */
        Gson gson = new Gson();
        TimesGet timesGet = null;
        /*对得到的请求数据进行封装*/
        Map parameterMap = req.getParameterMap();
        Iterator paIter  = parameterMap.keySet().iterator();
        while (paIter.hasNext()){
            String key = paIter.next().toString();
            String[] values = (String[]) parameterMap.get(key);
            timesGet = gson.fromJson(key,TimesGet.class);
        }
        Member member = new UserDaoImpl().getMember(timesGet.getTellphone());   //通过电话得到用户
        if (member != null){        //查询到消费的用户
            //查到该用户
            System.out.println(member.getUsername()+"正在充值次数...");
            /*计算充值理发项目次数的数值
            //剪发次数
            int cut = timesGet.getCutNum()-member.getHaircut();
            //黑油次数
            int black = timesGet.getBlackNum()-member.getBlack();
            //染发次数
            int colour = timesGet.getDyeNum()-member.getColour();
            //护理次数
            int protect = timesGet.getNuseNum()-member.getWash_protect();
            //烫发次数
            int perm = timesGet.getPerNum()-member.getPerm();
            //补发根次数
            int root = timesGet.getRootNum()-member.getHairroot();
            //洗发次数
            int wash = timesGet.getWashNum()-member.getWash();*/
            //更新用户充值后的次数
            member.setHaircut(timesGet.getCutNum());
            member.setBlack(timesGet.getBlackNum());
            member.setColour(timesGet.getDyeNum());
            member.setWash_protect(timesGet.getNuseNum());
            member.setPerm(timesGet.getPerNum());
            member.setHairroot(timesGet.getRootNum());
            member.setWash(timesGet.getWashNum());
            //更新次数
            if (new UserDaoImpl().updateMember(member) > 0)
            {
                System.out.println("充值成功");
            }else {
                System.out.println("充值失败");
            }
        }
    }
}
